package online.shenjian.hadoop.partitioner;

import online.shenjian.hadoop.flow.FlowBean;
import online.shenjian.hadoop.flow.FlowMapper;
import online.shenjian.hadoop.flow.FlowReduce;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author shenjian
 * @since 2023/2/6
 */
public class FlowPartitionerDriver {

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "flowPartitioner");
        job.setJarByClass(FlowPartitionerDriver.class);
        job.setMapperClass(FlowMapper.class);
        job.setCombinerClass(FlowReduce.class);
        job.setReducerClass(FlowReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);
        // 关联自定义分区类
        job.setPartitionerClass(ProvincePartitioner.class);
        // 设置ReduceTask任务数，保持与分区数一致
        job.setNumReduceTasks(5);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
